package com.stu.manager.studentscoremanagersys.controller;

import com.stu.manager.studentscoremanagersys.pojo.Student;
import com.stu.manager.studentscoremanagersys.req.ComonListRequest;
import com.stu.manager.studentscoremanagersys.req.ReqStudentAdd;
import com.stu.manager.studentscoremanagersys.req.ReqStudentUpdate;
import com.stu.manager.studentscoremanagersys.res.Result;
import com.stu.manager.studentscoremanagersys.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/student")
public class StudentHandler {

    @Autowired
    StudentService studentService;

    @PostMapping("/add")
    public Result<String> addStudent(@RequestBody @Validated ReqStudentAdd reqStudentAdd){
        studentService.add(reqStudentAdd);
        return Result.buildScuess("添加成功");
    }

    @PostMapping("/selectPage")
    public Result<List<Student>> selectPage(@RequestBody ComonListRequest commonVo){
        return studentService.selectPage(commonVo);
    }

    @GetMapping("/deleteById")
    public Result<String> selectPage(Long id){
        if (id== null){
            Result.buildFailure("学生id为空");
        }
        studentService.deleteById(id);
        return Result.buildScuess("删除学生成功！");
    }

    @GetMapping("/selectById")
    public Result selectById(Long id){
        if (id== null){
            Result.buildFailure("学生id为空");
        }
        return studentService.selectById(id);
    }

    @PostMapping("/updateById")
    public Result updateById(@RequestBody @Validated ReqStudentUpdate update){
        if (update == null){
            Result.buildFailure("学生信息不能为空");
        }
        studentService.updateById(update);
        return Result.buildScuess("更新学生成功！");

    }

    @GetMapping("/selectAll")
    public Result<List<Student>> selectAll(){
       return studentService.selectAll();
    }
}
